package com.company

import kotlin.math.roundToInt

const val NAME = "jimmy's friend"
const val NAMES = "jack,rose,luomiou"
fun main() {

    //1、substring   函数支持IntRange类型（一个整数范围的类型） until创建的范围不包括上限值
    val index = NAME.indexOf("\'")
//    val str = NAME.substring(0,index)
    val str = NAME.substring(0 until index)
    println(str)


    //2、split 返回的是list集合数据
    val data = NAMES.split(",")
    //data[0]

    val (str1, str2, str3) = NAMES.split(",")
    println("$str1 $str2 $str3")


    //3、replace

    val s1 = "The people's Republic of China."
    val s2 = s1.replace(Regex("[aeiou]")) {
        when (it.value) {
            "a" -> "8"
            "e" -> "6"
            "i" -> "9"
            "o" -> "1"
            "u" -> "3"
            else -> it.value
        }
    }
    println(s1)
    println(s2)


    //4、字符串比较
    // kotlin中用==比较内容是否相同，用===比较是否指向内存堆上同一对象
    //java中==比较是否指向内存堆上同一对象，equals比较内容是否相同
    val s3 = "Jason"
//    val s4 = "Jason"
    val s4 = "jason".capitalize()
    println(s3 == s4)
    println(s3 === s4)


    //5、字符串遍历
    NAME.forEach {
        print("$it*")
    }
    println("-------------")


    //6、安全转换函数
//    val number: Int? = "8.98".toInt() //Exception in thread "main" java.lang.NumberFormatException: For input string: "8.98"
    val number: Int? = "8.98".toIntOrNull()
    println(number) // null 如果不能正确转换，与其触发异常不如返回null


    //7、Double转Int
    println(8.956147.toInt()) //精度损失
    println(8.956147.roundToInt()) //四舍五入

    //8、Double类型格式化
    val s = "%.2f".format(8.956147)
    println(s)


}